#! /bin/sh

#####################################
# PREPARE variables:
#
# NOTE that "app"/Contents specify slash. 
# this is not a problem even if app itself have slash at end.
# We will get something as  gg.app//Contents and this still works fine.
#
app=$1
app_vcomp="$app"/Contents/vcomponents

# YOU can change this to 0 to skip VREPORT.dll 
install_vreports=1


#####################################
# COPY files of vcomponents into APP.
#####################################

if [ ! -d "$app_vcomp" ] ; then
	mkdir "$app_vcomp"
fi

# VSHARED
if [ ! -e "$app_vcomp"/libvshared_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvshared_fat_release.dylib "$app_vcomp"
fi

# VKERNEL
if [ ! -e "$app_vcomp"/libvkernel_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvkernel_fat_release.dylib "$app_vcomp"
fi

# VCLIENT
if [ ! -e "$app_vcomp"/libvclient_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvclient_fat_release.dylib "$app_vcomp"
fi

# VREPORT
if [ ! -e "$app_vcomp"/libvreport_fat_release.dylib ] ; then
	cp /usr/local/lib/vcomponents/libvreport_fat_release.dylib "$app_vcomp"
fi


####################################
# Copy Folders:
####################################

# vresources
cp -R /usr/local/lib/vcomponents/vresources "$app_vcomp"


#####################################
# CHANGE executable_path for engine dylibs:
#####################################

	# change id of VSHARED
	install_name_tool -id "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvshared_fat_release.dylib

	# change id of VKERNEL
	install_name_tool -id "@executable_path/../vcomponents/libvkernel_fat_release.dylib" "$app_vcomp"/libvkernel_fat_release.dylib

	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"   "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvkernel_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/VSDK.framework/Versions/A/VSDK" "@executable_path/../vcomponents/VSDK.framework/Versions/A/VSDK" 	"$app_vcomp"/libvkernel_fat_release.dylib

	# change id of VCLIENT
	install_name_tool -id "@executable_path/../vcomponents/libvclient_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" "@executable_path/../vcomponents/libvshared_fat_release.dylib" "$app_vcomp"/libvclient_fat_release.dylib

	# change id of VREPORT
	install_name_tool -id "@executable_path/../vcomponents/libvreport_fat_release.dylib" "$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib"  "@executable_path/../vcomponents/libvshared_fat_release.dylib" 	"$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib"  "@executable_path/../vcomponents/libvkernel_fat_release.dylib" 	"$app_vcomp"/libvreport_fat_release.dylib
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib"  "@executable_path/../vcomponents/libvclient_fat_release.dylib" 	"$app_vcomp"/libvreport_fat_release.dylib

    
#####################################
# now we need update yet App/Xtras/V4MD.xtra  dll to also point new location of vcomponents folder.
#####################################
v4md_file="$app"/../Xtras/V4MD.xtra/Contents/MacOS/V4MD

	#
	# now we can change paths In the V4MD.Xtra
	#
	install_name_tool -change "/usr/local/lib/vcomponents/libvshared_fat_release.dylib" 	"@executable_path/../vcomponents/libvshared_fat_release.dylib" 		"$v4md_file"
	install_name_tool -change "/usr/local/lib/vcomponents/libvkernel_fat_release.dylib" 	"@executable_path/../vcomponents/libvkernel_fat_release.dylib" 		"$v4md_file"
	install_name_tool -change "/usr/local/lib/vcomponents/libvclient_fat_release.dylib" 	"@executable_path/../vcomponents/libvclient_fat_release.dylib" 		"$v4md_file"
	install_name_tool -change "/usr/local/lib/vcomponents/libvreport_fat_release.dylib" 	"@executable_path/../vcomponents/libvreport_fat_release.dylib" 		"$v4md_file"

